<?php
add_action( 'after_setup_theme', 'singcrets_theme_setup' );

function singcrets_theme_setup() {
    remove_filter( 'get_the_excerpt', 'twentyten_custom_excerpt_more' );
    remove_filter( 'excerpt_more', 'twentyten_auto_excerpt_more');
    add_filter('excerpt_more', 'singcrets_auto_excerpt_more');
    add_filter('get_the_excerpt', 'singcrets_custom_excerpt_more');
     // add_filter('the_excerpt', 'singcrets_custom_excerpt_more');
	
	// global $current_single_idx;

	// Limit posts displayed for front-page
	add_filter( 'post_limits', 'singcrets_post_limits' );

	// Include slider js to header
	add_action('wp_enqueue_scripts', 'singcrets_scripts_method');

	// Remove version displayed on js file 
	add_filter('script_loader_src', 'singcrets_script_loader_filter');

	add_filter('wp_head', 'singcrets_head');
	
	add_filter('init', 'add_custom_image_size');
}


function singcrets_custom_excerpt_more( $output ) {
	if ( has_excerpt() && ! is_attachment() ) {
		$output .= singcrets_continue_reading_link();
	}
	return $output;
}

function singcrets_continue_reading_link() {
	return ' <a class=\'read-more\' href="'. get_permalink() . '">' . 'Read more' . '</a>';
}


function singcrets_auto_excerpt_more( $more ) {
	return ' &hellip;' . singcrets_continue_reading_link();
}


function add_custom_image_size() {
	add_image_size( 'post-thumbnail', '320', '280', true );
	add_image_size( 'large-feature', '1000', '288', true );
	add_image_size( 'small-feature', '500', '300', false );
	add_image_size( 'home-thumb', '320', '280', true );
}







function singcrets_head() {
	$large_image_url =  get_bloginfo('stylesheet_directory') . "/images/home-default-image.png";
	if ( has_post_thumbnail()) {
	  $large_image_urls = wp_get_attachment_image_src( get_post_thumbnail_id(), 'home-thumb');
	  $large_image_url = $large_image_urls[0];
	}
	?>
	<meta property="og:title" content="<?php the_title(); ?>" />
	<meta property="og:description" content="<?php the_excerpt_rss(); ?>" />
	<meta property="og:image" content="<?php echo $large_image_url; ?>" />
	<?php
}

function singcrets_post_limits( $limit ) {
    if ( is_front_page() ) {
        return 'LIMIT 0, 5';
    }
    return $limit;
}




function singcrets_script_loader_filter($src)
{

    if ( FALSE === strpos($src, 'js/jquery/jquery.js') )
    {
		$new_src = explode('?', $src);
		return $new_src[0];
        
    }

	return $src;
}

function remove_shortcode_from_index($content) {
	$content = str_replace('Share this:', '',$content);
	$content = substr($content, 0 ,50);
	return $content;
}
add_filter('the_excerpt_rss', 'remove_shortcode_from_index');


function singcrets_scripts_method() {
	if(is_single() || is_category() || is_page('newsletter') || is_search()){
		wp_enqueue_script(
			'slides-script',
			get_bloginfo('stylesheet_directory'). '/js/slides.min.jquery.js',
			array('jquery')
		);
	}
	
	if(is_front_page()){
		wp_enqueue_script(
			'roundabout-script',
			get_bloginfo('stylesheet_directory'). '/js/jquery.roundabout.js',
			array('jquery')
		);
	}
	if(!is_admin()) {
		wp_enqueue_script(
			'custom-script',
			get_bloginfo('stylesheet_directory'). '/js/custom-scripts.js',
			array('jquery')
		);
	}
	js_localize('singcrets_global_vars', array('siteurl' => get_bloginfo('siteurl')));
	
	}

	/*
	
	wp_localize_script( 'global_variable_script', 'singcrets_global_variable', array('site_url' => get_bloginfo('stylesheet_directory')));
	
	wp_enqueue_script('global_variable_script');
	*/

	

// Regist Footer Menu Container
register_nav_menu( 'secondary', 'Footer Menu' );

// Regist Sidebar on Single Page
register_sidebar( array(
    'id'          => 'sidebar-single',
    'name'        => __( 'Singles Page Sidebar'),
	'before_widget' => '<aside id="%1$s" class="widget %2$s">',
	'after_widget' => "</aside>\n",
	'before_title' => '<h3 class="widget-title">',
	'after_title' => "</h3>\n"
) );


function add_body_class( $classes )
{
    global $post;
    if ( isset( $post ) ) {
        $classes[] = $post->post_type . '-' . $post->post_name;
    }
    return $classes;
}
add_filter( 'body_class', 'add_body_class' );

add_filter('the_content', 'singcrets_automaticaly_add_shortcode', 10, 1);
add_filter('the_content', 'singcrets_automaticaly_add_tag', 10, 1);

function singcrets_automaticaly_add_tag( $content ) {
	$tagshtml = "";
	$categories = get_the_tags();
	$separator = ', ';
	$output = '';
	// print_r($categories);
	
	if($categories){
		$output = '<span style="font-size: x-small;">Tags : ';
		foreach($categories as $category) {
			// $output .= '<a href="'.get_tag_link($category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->name.'</a>'.$separator;
			$output .= $category->name . $separator;
		}
	$output = substr($output, 0, strripos($output, $separator));
	// print_r(strripos($output, $separator));
	$output .= '</span><br/>';
	// $output = trim($output, $separator);
	
	}
	
	
	return $content . '<br/>' . $output;
}
function singcrets_automaticaly_add_shortcode( $content ) {
	$temppos = strpos($content,'[sharethis]');
	
	//If exist - Do nothing
	if(!$temppos && !is_page()) {
		return $content . '[sharethis]';
	}
	return $content;
}

require_once ( 'shortcode.php' );
function wpcf7_send_to_mailchimp($cfdata) {
	/*
	
	[posted_data] => Array
        (
            [_wpcf7] => 50
            [_wpcf7_version] => 3.3.1
            [_wpcf7_unit_tag] => wpcf7-f50-p2-o1
            [_wpnonce] => e4d8f2d042
            [your-email] => lehoaian.it@gmail.com
            [radio-788] => a Girl
            [text-date-d] => 16
            [text-date-m] => 10
            [text-date-y] => 88
            [_wpcf7_is_ajax_call] => 1
        )
	*/
	$formtitle = $cfdata->title;
	$formdata = $cfdata->posted_data;
	$send_this_email = $formdata['your-email'];
	$b_day = $formdata['text-date-d'] . '/' . $formdata['text-date-m'] . '/' . $formdata['text-date-y'] ;
	$mergeVars = array(
		'EMAIL'=>$formdata['your-email'],
		'SEX'=>$formdata['radio-788'],
		'BIRTHDAY'=>$b_day
	);

	// MCAPI.class.php needs to be in theme folder
	require_once('MCAPI.class.php');

	// grab an API Key from http://admin.mailchimp.com/account/api/
	$api = new MCAPI('ab114f2e27c829c7bc073134f3fe1ccf-us6');

	// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
	// Click the "settings" link for the list - the Unique Id is at the bottom of that page.
	$list_id = "a0b01fae4c";

	// Send the form content to MailChimp List without double opt-in
	$retval = $api->listSubscribe($list_id, $send_this_email, $mergeVars, 'html', false);
	
	// print_r($retval);
}
add_action('wpcf7_mail_sent', 'wpcf7_send_to_mailchimp', 1);

